package
{
	import com.boostworthy.display.ZBuffer;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageDisplayState;
	import flash.display.StageQuality;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	
	[SWF(backgroundColor="#FFFFFF", frameRate="60", width="550", height="400")]

	public class Example extends Sprite
	{
		public var plane1:Sprite;
		
		public var plane2:Sprite;
		
		public var plane3:Sprite;
		
		protected var _zBuffer:ZBuffer;
		
		protected var _angle:Number = 0;
		
		public function Example()
		{
			init();
		}
		
		protected function init():void
		{
			configStage();
			
			_zBuffer = new ZBuffer();
			
			plane1 = new Sprite();
			plane1.graphics.beginFill(0xFF0000);
			plane1.graphics.drawRect(0, 0, 200, 200);
			plane1.graphics.endFill();
			
			plane2 = new Sprite();
			plane2.graphics.beginFill(0x00FF00);
			plane2.graphics.drawRect(0, 0, 200, 200);
			plane2.graphics.endFill();
			
			plane3 = new Sprite();
			plane3.graphics.beginFill(0x0000FF);
			plane3.graphics.drawRect(0, 0, 200, 200);
			plane3.graphics.endFill();
			
			plane1.x = 80;
			plane1.y = 50;
			plane1.z = 100;
			
			plane2.x = 150;
			plane2.y = 100;
			plane2.z = 200;
			
			plane3.x = 220;
			plane3.y = 150;
			plane3.z = 20;
			
			addChild(plane1);
			addChild(plane2);
			addChild(plane3);
			
			addEventListener(Event.ENTER_FRAME, enterFrameHandler, false, 0, true);
		}
		
		protected function configStage():void
		{
			stage.align 					= StageAlign.TOP_LEFT;
			stage.displayState 				= StageDisplayState.NORMAL;
			stage.quality 					= StageQuality.HIGH;
			stage.scaleMode 				= StageScaleMode.NO_SCALE;
			stage.showDefaultContextMenu	= false;
		}
		
		protected function enterFrameHandler(event:Event):void
		{
			plane1.z = 300 + Math.cos(_angle)     * 300;
			plane2.z = 300 + Math.cos(_angle + 1) * 300;
			plane3.z = 300 + Math.cos(_angle + 2) * 300;
			
			_angle += 0.05;
			
			_zBuffer.recursiveSort(this);
		}
	}
}
